﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentWeb.DAL;
using StudentWeb.Model;
using System.Security.Cryptography;

namespace StudentWeb.BLL
{
    public class XhVipBO
    {
        public IResult Login(string XhVipName, string XhVipPassword)
        {
            var res = new XhVipDataDAO().GetInfoByVipName(XhVipName);
            if (res != null)
            {
                if (XhVipPassword == res.XhVipPassword) return new Result(ResultCode.Success, "登录成功");
                else
                {
                    return new Result(ResultCode.Fail, "登录失败");

                }
            }
            else
            {
                return new Result(ResultCode.Fail, "登录失败");
            }
        }


        #region 教师信息 保存
        public bool SaveXhVipInfo(dynamic data, int XhVipDataID)
        {
            int xhvipID = data.XhVipDataID ?? 0;
            var result = 0;
            if (xhvipID > 0)
            {

            }
            else if (xhvipID == 0)
            {
                string salt = Guid.NewGuid().ToString("N").Substring(0, 6);
                string pwd = data.UserPassword;
                var xhvip = new XhVipData()
                {
                    //TeacherState = data.TeacherState,
                    XhVipPassword = Sha256Encrypt(salt, pwd),
                    XhVipName = data.XhVipName,

                };
                result = new XhVipDataDAO().Insert(xhvip);
            }
            return result > 0;
        }
        #endregion


        private static string Sha256Encrypt(string salt, string password)
        {
            SHA256Managed sha256 = new SHA256Managed();
            UTF8Encoding utf8Encode = new UTF8Encoding();
            byte[] hashData = sha256.ComputeHash(utf8Encode.GetBytes(salt + password));
            sha256.Clear();
            return Convert.ToBase64String(hashData);
        }
    }

}